package com.trkj.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.ContractDAO;
import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.PositionDAO;
import com.trkj.dao.QuitDAO;
import com.trkj.pojo.Contract;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Position;
import com.trkj.pojo.Quit;
import com.trkj.vo.QuitVo;
/**
 * 离职申请
 * @author 彭帅
 *
 */
@Service
@Transactional
public class QuitService {
	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	PositionDAO positionDAO;
	@Autowired
	ContractDAO contractDAO;
	@Autowired
	QuitDAO quitDAO;
	public QuitVo quitVo(Integer empId) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			QuitVo quitVo = new QuitVo();
			Emp emp = empDAO.findOne(empId);
			quitVo.setEmpId(emp.getEmpId());
			quitVo.setEmpName(emp.getEmpName());
			System.out.println(emp);
			if (emp.getDept()!=null) {
				quitVo.setDeptId(emp.getDept().getDeptId());
				quitVo.setDeptName(emp.getDept().getDeptName());
			}
			if (emp.getPosition()!=null) {
				quitVo.setPositionId(emp.getPosition().getPositionId());
				quitVo.setPositionName(emp.getPosition().getPositionName());
			}
			Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
			int a = date.getYear()+1900;
			int b = date.getMonth()+1;
			int c = date.getDate();
			quitVo.setQuitUptime(a+"-"+b+"-"+c);
			quitVo.setLunchPeople(emp.getEmpId());
			return quitVo;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 离职申请暂存
	 * @param quit
	 * @param empId
	 * @param deptId
	 * @param positionId
	 * @return
	 */
	public Integer save_quit(Quit quit,Integer empId,Integer deptId,Integer positionId) {
		try {
			if (quit != null) {
				if (empId != null) {
					Emp emp = empDAO.findOne(empId);
					quit.setEmp(emp);
				}
				if (deptId != null) {
					Dept dept = deptDAO.findOne(deptId);
					quit.setDept(dept);
				}
				if (positionId != null) {
					Position position = positionDAO.findOne(positionId);
					quit.setPosition(position);
				}
				return quitDAO.save(quit).getQuitId();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	/**
     * 修改离职人员的信息
     * @author 
     * @param quitId
     * @return
     */
    public boolean updateEmpstateInfo(Integer quitId) {
    	try {
			if (quitId!=null) {
				Quit quit = quitDAO.findOne(quitId);
				Integer empId = quit.getEmp().getEmpId();
				if (empId!=null) {
					Emp emp = empDAO.findOne(empId);
					Contract contract = contractDAO.findOne(empId);
					 if(emp.getEmpState()==1) {
						 emp.setEmpState(0);
						 emp.setEmpLeavetime(quit.getQuitTime());	
						 if (contract!=null && contract.getContractState()=="有效" ) {
							 contract.setContractState("解除");
						 }
						 return true;
					 }
					
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }

}
